<?php

declare(strict_types=1);

namespace addons\Gallery\driver;

use OSS\OssClient;
use OSS\Core\OssException;
use think\App;
use think\facade\Db;
use app\common\tool\Result;

class Aliyunoss
{
    /**
     * @var string 配置文件
     */
    public array $config;

    /**
     * @var object|\think\Request
     */
    public object $request;

    /**
     * @var object|App
     */
    public object $app;

    /**
     * @var string 图库表
     */
    public string $db = 'gallery';

    /**
     * @var string 图库分类表
     */
    public string $cate = 'gallery_cate';
    /**
     * 阿里云accessKeyId
     */
    public string $accessKeyId;
    /**
     * 阿里云accessKeySecret
     */
    public string $accessKeySecret;
    /**
     * 阿里云bucket
     */
    public string $bucket;
    /**
     * 阿里云bucket
     */
    public string $endpoint;
    /**
     * 域名
     */
    public string $domain;
    public function __construct()
    {
        $this->request = app('request');
        $this->app = app();
        $this->config = $this->app->config->get('gallery')['aliyun'];
        /**
         * 配置文件
         */
        $this->accessKeyId = $this->config['accessKeyId'];
        $this->accessKeySecret = $this->config['accessKeySecret'];
        $this->bucket = $this->config['bucket'];
        $this->endpoint = $this->config['endpoint'];
        $this->domain = $this->config['domain'];
    }
    public function uploads()
    {

        try {
            $file = request()->file('file');
            $res = explode('.', $file->getOriginalName());
            $name = $res[0] . filegenerate(32) . '.' . $res[1];
            $object = "video/".$name;
            $filePath = $file;
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $resossClient = $ossClient->uploadFile($this->bucket, $object, $filePath);
            $url = $this->domain.$object;
            $data = [
                'key'=>$object,
                'url'=>$url
            ];
            return Result::success('上传成功',$data);
        } catch (OssException $e) {
            return Result::fail($e->getMessage());
        }catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    public function addVideo()
    {
        try {
            $param = $this->request->post();
            Db::name($this->db)->insert([
                'title'       => $param['title'],
                'url'         => $param['key'],
                'cate'        => $param['cate'],
                'type'        => 2,
                'create_time' => date('Y-m-d H:i:s'),
                'update_time' => date('Y-m-d H:i:s')
            ]);
            return Result::success('上传成功');
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
    /**
     *删除
     */
    public function deleteVideo()
    {

        try {
            $param = $this->request->post();
            if(!isset($param['url'])){
                return Result::fail('参数错误');
            }
            $res = explode('/', $param['url']);
            $object = $res[3].'/'.$res[4];
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);
            $resossClient = $ossClient->deleteObject($this->bucket, $object);
            return Result::success('删除成功');
        } catch (OssException $e) {
            return Result::fail($e->getMessage());
        }catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }
}
